package com.yricky.memoria.ui.component.common

import android.app.Activity
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.material.DropdownMenu
import androidx.compose.material.Icon
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.ArrowBack
import androidx.compose.material.icons.rounded.MoreVert
import androidx.compose.runtime.Composable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.yricky.memoria.data.pojo.PdfQuery
import com.yricky.memoria.ui.theme.Shapes
import com.yricky.memoria.utils.PdfDoc
import com.yricky.memoria.utils.startPdfActivity

/**
 * @author Yricky
 * @date 2021/12/11
 */

@Composable
fun <T> HorizontalList(
    list:List<T>,
    title:@Composable ()->Unit = {},
    item: @Composable (T)->Unit,
    emptyContent:@Composable ()->Unit = {}
){
    Row(
        modifier = Modifier
            .padding(12.dp, 4.dp),
        verticalAlignment = Alignment.CenterVertically
    ){
        title()
        LazyRow{
            items(list){ obj ->
                item(obj)
            }
            if(list.isEmpty()){
                item{
                    emptyContent()
                }
            }
        }

    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun HorizontalPdfList(
    list:List<Pair<String,PdfDoc>>,
    onItemClick:(Pair<String,PdfDoc>)->Unit,
    menu:@Composable ColumnScope.(MutableState<Boolean>,item:Pair<String,PdfDoc>)->Unit = {_,_-> }
){

    HorizontalList(list = list,
        title = {
            Text(text = "PDF：")
        },
        item = {
            val expandState = remember {
                mutableStateOf(false)
            }
            Surface(
                modifier = Modifier
                    .height(120.dp)
                    .width(100.dp)
                    .padding(4.dp),
                shape = Shapes.small) {
                Column(
                    modifier = Modifier
                        .background(Color(0x33808080))
                        .combinedClickable(
                            onClick = { onItemClick(it) },
                            onLongClick = { expandState.value = !expandState.value }
                        )
                        .padding(8.dp),
                    horizontalAlignment = Alignment.CenterHorizontally) {
                    PdfPreview(
                        modifier = Modifier.weight(1f),it.second,0)
                    Text(
                        text = it.first,
                        fontSize = 10.sp,
                        maxLines = 2
                    )
                }

                DropdownMenu(
                    modifier = Modifier
                        .wrapContentWidth()
                        .wrapContentHeight(),
                    expanded = expandState.value,
                    onDismissRequest = {
                        expandState.value = false
                    },
                    offset = DpOffset(0.dp, 0.dp),
                ) {
                    menu(this,expandState,it)
                }
            }
        }
    )
}

@Preview(showBackground = true)
@Composable
fun Split(){
    Spacer(modifier = Modifier
        .fillMaxWidth()
        .height(1.dp)
        .background(Color(0x80808080)))
}


@Composable
fun ItemContentFrameLimited(
    w:Dp = 700.dp,
    isEdit:Boolean = false,
    contentEdit:@Composable RowScope.()->Unit = {},
    content: @Composable RowScope.()->Unit
){
    Column(
        Modifier
            .padding(4.dp)
            .fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        if(isEdit){
            Row(
                Modifier
                    .widthIn(max = w)
                    .wrapContentHeight(),
            ) { contentEdit(this) }
        }
        Row(
            Modifier
                .widthIn(max = w)
                .wrapContentHeight(),
            verticalAlignment = Alignment.CenterVertically
        ){ content(this) }
    }
}

@Composable
fun ItemContentFrame(
    isEdit:Boolean = false,
    contentEdit:@Composable RowScope.()->Unit = {},
    content: @Composable RowScope.()->Unit
){
    Column(
        Modifier
            .padding(4.dp)
            .fillMaxWidth(),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        if(isEdit){
            Row(
                Modifier
                    .wrapContentHeight(),) { contentEdit(this) }
        }
        Row(
            Modifier
                .wrapContentHeight(),
            verticalAlignment = Alignment.CenterVertically
        ){

            content(this)
        }
    }
}


@Preview(showBackground = true)
@Composable
fun Title(title:String = "title",menu:@Composable ColumnScope.(MutableState<Boolean>)->Unit = {}){
    val expandState = remember {
        mutableStateOf(false)
    }
    Column {
        Row(
            Modifier
                .fillMaxWidth()
                .height(40.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            val activity = (LocalContext.current as? Activity)
            Icon(
                Icons.Rounded.ArrowBack,"",
                modifier = Modifier
                    .clickable { activity?.onBackPressed() }
                    .padding(10.dp))
            Spacer(modifier = Modifier.weight(1f))
            Text(
                text = title,
                fontSize = 20.sp,
                maxLines = 1
            )
            Spacer(modifier = Modifier.weight(1f))
            Box {
                Icon(
                    Icons.Rounded.MoreVert,"",
                    modifier = Modifier
                        .clickable {
                            expandState.value = !expandState.value

                        }
                        .padding(10.dp))
                DropdownMenu(
                    modifier = Modifier
                        .wrapContentWidth()
                        .wrapContentHeight(),
                    expanded = expandState.value,
                    onDismissRequest = {
                        expandState.value = false
                    },
                    offset = DpOffset(0.dp, 0.dp),
                ) {
                    menu(this,expandState)
                }
            }
        }
        Split()
    }

}